package edu.cuit.zhuyimeng.flight.config;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheManager;
import edu.cuit.zhuyimeng.flight.controller.vo.FlightInfoVO;
import edu.cuit.zhuyimeng.flight.enums.CacheConstants;
import edu.cuit.zhuyimeng.framework.cache.util.CacheUtils;
import edu.cuit.zhuyimeng.framework.common.system.enums.FlightStatus;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 航班管理缓存配置类
 */
@Configuration
@RequiredArgsConstructor
public class CacheConfig {

    private final CacheManager cacheManager;

    /**
     * 航班信息缓存
     */
    private Cache<Long, FlightInfoVO> flightInfoCache;
    /**
     * 航班信息列表缓存以 出发时间(LocalDate)-出发地-目的地 作为键
     */
    private Cache<String, List<Long>> specificFlightInfoCache;
    /**
     * 航班信息分页缓存
     */
    private Cache<Integer,List<Long>> paginationFlightInfoCache;

    @PostConstruct
    public void init() {
        flightInfoCache = createCache(CacheConstants.FLIGHT_INFO);
        specificFlightInfoCache = createCache(CacheConstants.SPECIFIC_FLIGHT_INFO);
        paginationFlightInfoCache = createCache(CacheConstants.PAGE_FLIGHT_INFO);
    }

    @Bean
    public Cache<Long, FlightInfoVO> flightInfoCache() {
        return flightInfoCache;
    }

    @Bean
    public Cache<String, List<Long>> specificFlightInfoCache() {
        return specificFlightInfoCache;
    }

    @Bean
    public Cache<Integer,List<Long>> paginationFlightInfoCache() {
        return paginationFlightInfoCache;
    }

    private <A,B> Cache<A,B> createCache(String name) {
        return cacheManager.getOrCreateCache(CacheUtils.createCommonCache(name));
    }

}
